<?php

class Device extends CI_Model {
    function __construct()
    {
        parent::__construct();
    }
    public function getDevices(){ 
	//goplaydb 
	//$query = $this->db->query('Select * from  Maitai_Gadgets');
	//return $query->result_array();
	//return $this->getPhonesFromDB();
	return $this->getPhonesFromShop();
	
   }
   
   public function getPhonesFromDB() {
   	
   		$this->config->load('config_maitai_tables');
   	
		// Check if DB TBL 
   		$this->db->cache_on();
   		$tbl_phones = $this->config->item('table_phones');
		$query = $this->db->get($tbl_phones);
		
   		if($q = $query->result_array())
		{
			// Has existing content
			//print_r($q); die();
			return $q;
			
		} else {
			// Has no item inside TBL

			// connect to Hessian to fetch new items
			$gadgetsFromAPI = $this->getPhonesFromAPI();
			//var_dump($gadgetsFromAPI);
			//die();
			// truncate TOUCH MOOOBB
			$this->db->truncate($tbl_phones);
			
			// store to DB
			$dataArr = array();
			foreach($gadgetsFromAPI as $gadget) {
				$data['gadgetId'] = $gadget->gadgetId;
				$data['gadgetName'] = $gadget->gadgetName;
				$data['gadgetBrand'] = $gadget->gadgetBrand;
				$data['gadgetPrice'] = $gadget->gadgetPrice;
				$data['gadgetPlanPrice'] = $gadget->gadgetPlanPrice;
				$data['gadgetSKU'] = $gadget->gadgetSKU;
				$data['gadgetImage'] = $gadget->gadgetImage;				
				array_push($dataArr, $data);
			}
			
			$this->db->insert_batch($tbl_phones, $dataArr); 

			//print_r($dataArr);
			$q = $dataArr;
		}
		
		$this->db->cache_off();
		$this->db->close();
		return $q;
   }
   
   public function getPhonesFromAPI($name="", $brand="", $minPrice=0, $maxPrice=10000)
   {
   		$this->load->library('Maitaidata', '', 'Mtdata');
   		$gadgets = $this->Mtdata->getGadgetList($name, $brand, $minPrice, $maxPrice);
   		$status = $gadgets->status;
   		if($status==0) {
   			// Has return object
   			return $gadgets->obj;
   		} else {
   			return false;
   		}
   }
   
   public function getPhonesFromShop()
   {
	   	// Get Prod info from SHOP
   		$phonesFromDB = $this->getPhonesFromDB();
   		$skuArr = $extraArr = $extraTempArr = $finalProductArray = $tempProductArray = array();
		foreach($phonesFromDB as $phone)
		{
			$sku = $phone['gadgetSKU'];
			$extraTempArr['brand'] = $phone['gadgetBrand'];
			$extraTempArr['planprice'] = $phone['gadgetPlanPrice'];
			$extraTempArr['price'] = $phone['gadgetPrice'];
			array_push($skuArr, $sku);
			array_push($extraArr, $extraTempArr);
		}
   		
		// Build curl URL
		$this->config->load('config_maitai_shopapi');
   		$curlUrl = $this->config->item('maitai_shopapi_reco_url');
   		$bulkurl = $curlUrl . implode("/", $skuArr);
		
		$ch = curl_init($bulkurl);    // initialize curl handle
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		$product_array = curl_exec($ch);  
		$productsFromShop = json_decode($product_array);
		
		// LOOP THROUGH EACH SHOP ITEM
		$ctr = 0;
		foreach($productsFromShop as $product)
		{
			// SKIP ITEM IF NOT EXISTING IN SHOP
			if(empty($product)) continue;
			
			// Values from SHOP API
			$tempProductArray['name'] = $product->name;
			$tempProductArray['imageurl'] = $product->imageurl;
			$tempProductArray['thumbnail'] = $product->thumbnail;
			$tempProductArray['shortdescription'] = $product->shortdescription;
			$tempProductArray['like'] = $product->like;
			
			// Values from Hessian/DB
			$tempProductArray['brand'] = $phonesFromDB[$ctr]['gadgetBrand'];
			$tempProductArray['sku'] = $phonesFromDB[$ctr]['gadgetSKU'];
			$tempProductArray['id'] = $phonesFromDB[$ctr]['gadgetId'];
			$tempProductArray['price'] = $phonesFromDB[$ctr]['gadgetPrice'];
			$tempProductArray['planprice'] = $phonesFromDB[$ctr]['gadgetPlanPrice'];
			
			// SAVE TO ARRAY NOW
			array_push($finalProductArray, $tempProductArray);
			$ctr++;
		}
		

		//print_r($finalProductArray);
		
		//print_r($product);
		return $finalProductArray;   		
   }
   
   
   public function getDevicePriceInDB($sku) {
   	
   		$this->config->load('config_maitai_tables');
   	
		// Check if DB TBL 
   		$this->db->cache_on();
   		$tbl_phones = $this->config->item('table_phones');
		$query = $this->db->get_where($tbl_phones, array('gadgetSKU'=>$sku));
		
   		if($q = $query->result_array())
		{
			// Has existing content
			//print_r($q); die();
			return $q[0]['gadgetPlanPrice'];
		}
		
		return false;
   }
   
}